package com.example.retrofitdemo616lixiao.activities;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;

import com.example.retrofitdemo616lixiao.R;
import com.example.retrofitdemo616lixiao.fragments.BaseFragment;
import com.example.retrofitdemo616lixiao.fragments.Fragment1;
import com.example.retrofitdemo616lixiao.fragments.Fragment2;
import com.example.retrofitdemo616lixiao.fragments.Fragment3;
import com.example.retrofitdemo616lixiao.fragments.Fragment4;
import com.example.retrofitdemo616lixiao.fragments.Fragment5;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private SharedPreferences sp;
    private String sessionID = "";

   private ViewPager viewPager;
   private FragmentPagerAdapter fragmentPagerAdapter;
   private BottomNavigationView navigationView;
   private List<BaseFragment> fragmentList;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initFragmentList();//初始化FragmentList
        initViewPager();//初始化ViewPager
        initBottomNV();//初始化BottomNavigationView

    }

    private void initFragmentList() {
        // 将fragment加载到list中
        fragmentList = new ArrayList<>();
        fragmentList.add(new Fragment1());
        fragmentList.add(new Fragment2());
        fragmentList.add(new Fragment3());
        fragmentList.add(new Fragment4());
        fragmentList.add(new Fragment5());
    }

    private void initViewPager() {
        //实例化viewpager
        viewPager = findViewById(R.id.viewpager);
        //实例化FragmentPagerAdapter
        fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };
        //设置viewpager的适配器
        viewPager.setAdapter(fragmentPagerAdapter);
        //设置viewpager的页面切换事件
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                System.out.println("---position=" + position);
                switch (position) {
                    case 0:
                        navigationView.setSelectedItemId(R.id.blog);
                        break;
                    case 1:
                        navigationView.setSelectedItemId(R.id.keynote);
                        break;
                    case 2:
                        navigationView.setSelectedItemId(R.id.video);
                        break;
                    case 3:
                        navigationView.setSelectedItemId(R.id.sample);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private void initBottomNV() {
        //实例化BottomNavigationView
        navigationView = findViewById(R.id.bottomnv);
        //设置BottomNavigationView切换事件
        navigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int id = 0;
                switch (item.getItemId()) {
                    case R.id.blog:
                        id = 0;
                        break;
                    case R.id.keynote:
                        id = 1;
                        break;
                    case R.id.video:
                        id = 2;
                        break;
                    case R.id.sample:
                        id = 3;
                        break;
                }
                System.out.println("---id=" + id);
                viewPager.setCurrentItem(id);
                return true;

            }
        });
    }



      /*  private EditText etUser,etPass;
        private Button btnlogin,btnlgin1;
        private String username="",password="",sessionID = "";
        private Switch sw;
        private SharedPreferences sp;


        private LoginListener loginListener=new LoginListener() {
            @Override
            public void onResponse(UserBean userBean) {
                sessionID=userBean.getSessionid();
                Log.i("MainActivity","--sessionID="+sessionID);
                if (sessionID!=null){
                    Toast.makeText(MainActivity.this,"登陆成功"+sessionID, Toast.LENGTH_SHORT).show();
                    //进入主界面
                    Intent intent=new Intent(MainActivity.this,NewActivity.class);
                    startActivity(intent);
                    finish();
                }
                else
                    Toast.makeText(MainActivity.this,"登陆失败",Toast.LENGTH_SHORT).show();

            }


            @Override
            public void onFail(String msg) {

            }
        };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        initViews();
        sp=getSharedPreferences("login",MODE_PRIVATE);
        readSP();
    }

    private void readSP() {
        String name=sp.getString("username","");
        String pass=sp.getString("password","");
        boolean remember=sp.getBoolean("remember",false);
        if (remember){
            etUser.setText(name);
            etPass.setText(pass);
        }

    }

    private void initViews() {
        etUser=findViewById(R.id.editText);
        etPass=findViewById(R.id.editText2);
        btnlogin=findViewById(R.id.button);
        btnlgin1=findViewById(R.id.button2);
        sw=findViewById(R.id.switch1);
        btnlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //登陆的代码
                username=etUser.getText().toString();
                password=etPass.getText().toString();
                LoginModel loginModel=new LoginModel();
                loginModel.getLoginResult(username,password,loginListener);
                Retrofit retrofit=new Retrofit.Builder()
                        .baseUrl("http://amicool.neusoft.edu.cn/")
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                UserService userService=retrofit.create(UserService.class);
                Call<UserBean> call=userService.login(etUser.getText().toString(),etPass.getText().toString());
                call.enqueue(new Callback<UserBean>() {
                    @Override
                    public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                        if (response.isSuccessful()&&response.body()!=null&&response.body().getId()!=null){
                            //记住密码
                            saveSP();
                            Toast.makeText(MainActivity.this,"登陆成功："+response.body().getSessionid(),Toast.LENGTH_SHORT).show();
                        }

                        else Toast.makeText(MainActivity.this,"登陆失败：",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(Call<UserBean> call, Throwable t) {
                        Toast.makeText(MainActivity.this,"网络错误",Toast.LENGTH_SHORT).show();
                    }
                });
            }

        });
        btnlgin1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //注册的代码
                Intent intent =new Intent(MainActivity.this,ZhuceActivity.class);
                startActivity(intent);
                onPause();
            }
        });
    }

    private void saveSP() {
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("username",etUser.getText().toString());
        editor.putString("password",etPass.getText().toString());
        editor.putBoolean("remember",sw.isChecked());
        editor.commit();

    }*/

}
